package no.rodland.acc.model;

import org.joda.time.DateTime;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import no.rodland.acc.db.AbstractRow;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jun 18, 2010 12:04:44 PM
 * @Author: fmr
 */
public class BudgetAccountGroup extends AbstractRow {
    private BudgetAccount budgetAccount;
    private AccountGroup accountGroup;

    protected BudgetAccountGroup(ResultSet rs,
                                 Map<Integer, BudgetAccount> budgetAccounts,
                                 Map<Integer, AccountGroup> accountGroups) throws SQLException {
        super(rs.getInt("id"),
                new DateTime(rs.getTimestamp("updated_date")),
                new DateTime(rs.getTimestamp("created_date")));
        budgetAccount = budgetAccounts.get(rs.getInt("budget_account_id"));
        accountGroup = accountGroups.get(rs.getInt("account_group_id"));
    }

    @Override
    public String getTableName() {
        return "budget_account_groups";
    }

    @Override
    public Object[] getData() {
        return new Object[]{budgetAccount.getId(), accountGroup.getId()};
    }

    @Override
    public String[] getColumns() {
        return new String[]{"budget_account_id", "account_group_id"};
    }

    public BudgetAccount getBudgetAccount() {
        return budgetAccount;
    }

    public AccountGroup getAccountGroup() {
        return accountGroup;
    }

    @Override
    public boolean usesIdentity() {
        return true;
    }

    @Override
    public String getText() {
        return "";
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("BudgetAccountGroup");
        sb.append("{budgetAccount=").append(budgetAccount);
        sb.append(", accountGroup=").append(accountGroup);
        sb.append('}');
        return sb.toString();
    }

    @SuppressWarnings({"RedundantIfStatement"})
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        BudgetAccountGroup that = (BudgetAccountGroup) o;

        if (getId() != that.getId()) {
            return false;
        }
        if (accountGroup != null ? !accountGroup.equals(that.accountGroup) : that.accountGroup != null) {
            return false;
        }
        if (budgetAccount != null ? !budgetAccount.equals(that.budgetAccount) : that.budgetAccount != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = budgetAccount != null ? budgetAccount.hashCode() : 0;
        result = 31 * result + (accountGroup != null ? accountGroup.hashCode() : 0);
        result = 31 * result + getId();
        return result;
    }
}
